using MediatR;
using RBAC.Domain.SalesManage;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using RBAC.Write.Api.Application.Command.ERP.SalesManagement;

namespace RBAC.Write.Api.Application.Handler.ERP.SalesManagement
{
    public class UpdateCustomerCommandHandler : IRequestHandler<UpdateCustomerCommands, APIResult<int>>
    {
        private readonly IBaseRepository<Customer> customerRep;

        public UpdateCustomerCommandHandler(IBaseRepository<Customer> customerRep)
        {
            this.customerRep = customerRep;
        }

        public Task<APIResult<int>> Handle(UpdateCustomerCommands request, CancellationToken cancellationToken)
        {
            if (request == null || request.customer == null)
            {
                return Task.FromResult(new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "参数不能为空",
                    Data = 0
                });
            }

            var entity = request.customer;
            if (entity.Id <= 0)
            {
                return Task.FromResult(new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "参数无效：Id",
                    Data = 0
                });
            }

            try
            {
                // 1) 确认存在且未删除
                var exist = customerRep.QueryFirstOrDefault<Customer>(
                    "SELECT * FROM Customer WHERE Id = @Id AND IsDeleted = false",
                    new { Id = entity.Id });

                if (exist == null)
                {
                    return Task.FromResult(new APIResult<int>
                    {
                        Code = APIEnums.Fail,
                        Msg = "客户不存在或已删除",
                        Data = 0
                    });
                }

                // 2) 更新可变更字段（不改 Id、CreateTime、CreateUser、IsDeleted）
                var sql = @"UPDATE Customer SET
                                CustomerName = @CustomerName,
                                CustomerAlias = @CustomerAlias,
                                ShortName = @ShortName,
                                Province = @Province,
                                City = @City,
                                Address = @Address,
                                ZipCode = @ZipCode,
                                Phone = @Phone,
                                CreditLevel = @CreditLevel,
                                CreditLimit = @CreditLimit,
                                CustomerType = @CustomerType,
                                Remark = @Remark,
                                Status = @Status
                              WHERE Id = @Id AND IsDeleted = false";

                var rows = customerRep.Execute(sql, new
                {
                    entity.CustomerName,
                    entity.CustomerAlias,
                    entity.ShortName,
                    entity.Province,
                    entity.City,
                    entity.Address,
                    entity.ZipCode,
                    entity.Phone,
                    entity.CreditLevel,
                    entity.CreditLimit,
                    entity.CustomerType,
                    entity.Remark,
                    entity.Status,
                    entity.Id
                });

                return Task.FromResult(new APIResult<int>
                {
                    Code = rows > 0 ? APIEnums.Success : APIEnums.Fail,
                    Msg = rows > 0 ? "修改成功" : "修改失败",
                    Data = rows
                });
            }
            catch (Exception ex)
            {
                return Task.FromResult(new APIResult<int>
                {
                    Code = APIEnums.Fail,
                    Msg = "修改失败：" + ex.Message,
                    Data = 0
                });
            }
        }
    }
}
